package myapp.com.xm.myapplication.View;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Scroller;
//http://www.manongjc.com/detail/5-uqjiciwsnwvcdmd.html
public class MyViewLayout extends ViewGroup {
    private Scroller scroller;
    private int mScreenHeight;
    private int mLastY;
    private int mStart;
    private int mEnd;

    public MyViewLayout(Context context) {
        super(context);
        init(context);
    }

    public MyViewLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MyViewLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View view = getChildAt(i);
            measureChild(view, widthMeasureSpec, heightMeasureSpec);
        }
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }
    int offsetHeight=0;
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View childView = getChildAt(i);
            if (childView.getVisibility() != View.GONE) {
                childView.layout(l, offsetHeight, r, offsetHeight=offsetHeight+childView.getMeasuredHeight());
            }
        }
        MarginLayoutParams marginLayoutParams = (MarginLayoutParams) getLayoutParams();
        marginLayoutParams.height = mScreenHeight * count;
    }

    private void init(Context context) {
        scroller = new Scroller(context);
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        mScreenHeight = displayMetrics.heightPixels;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mLastY=(int)event.getY();
                mStart=getScrollY();
               return  true;
               case MotionEvent.ACTION_MOVE:
                   if(!scroller.isFinished()){
                       scroller.abortAnimation();
                   }
                   int offsetY= (int) (mLastY-event.getY());
                   if(getScrollY()+offsetY<0){
                       offsetY=0;
                   }
                   if(getScrollY()>getHeight()-mScreenHeight&& offsetY>0){
                       offsetY=0;
                   }
                   scrollBy(0,offsetY);
                   mLastY= (int) event.getY();
                   break;
            case MotionEvent.ACTION_UP:
                mEnd=getScrollY();
                int distance=mEnd-mStart;
                if(distance>0){
                    if(distance<mScreenHeight/3){
                        scroller.startScroll(0,getScrollY(),0,-distance);
                    }else{
                        scroller.startScroll(0,getScrollY(),0,mScreenHeight-distance);

                    }
                }else{
                    if(-distance<mScreenHeight/3){
                        scroller.startScroll(0,getScrollY(),0,-distance);
                    }else{
                        scroller.startScroll(0,getScrollY(),0,-mScreenHeight-distance);
                    }
                }
                postInvalidate();
        }
        return  super.onTouchEvent(event);
    }

    @Override
    public void computeScroll() {
        if(scroller.computeScrollOffset()){
            scrollTo(scroller.getCurrX(),scroller.getCurrY());
            postInvalidate();
        }
    }

}
